﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZK.Common;
using ZK.HardWareBLL;
using ZK.HardWareIBLL;

namespace ZK.YHP_TransManage.Test
{
    public partial class 地磅串口转tcp : Form
    {

        A9WagonBalanceBLL NBKLD39WagonBalance;
        SocketServerBLL socketServerBLL = null;
        public 地磅串口转tcp()
        {
            InitializeComponent();
            var ports = SerialPort.GetPortNames();
            // 端口
            comboBox1.DataSource = ports;
            comboBox1.SelectedIndex = Myconfig.myconfigValue.DuankouhaoIndex;
            // 波特率
            comboBox2.SelectedIndex = Myconfig.myconfigValue.BotelvIndex;

            comboBox3.SelectedIndex = Myconfig.myconfigValue.ShujuweiIndex;
            comboBox4.SelectedIndex = Myconfig.myconfigValue.TingzhiweiIndex;
            comboBox5.SelectedIndex = Myconfig.myconfigValue.XiaoyanweiIndex;
            textBoxPort.Text = Myconfig.myconfigValue.Port;

        }
        string tempstr = "";
        private void WeightCallback(byte[] byt)
        {
            this.Invoke(new Action(() =>
            {
                int leg = textBox1.TextLength;
                string infoTest = textBox1.Text;
                if (leg > 5000)
                {
                    infoTest = infoTest.Substring(leg / 5);
                    textBox1.Text = infoTest;
                }
                textBox1.AppendText(DateTime.Now.ToString("HH:mm:ss")+" "+ string.Join("-", byt)+ "\r\n");
            }));

        }


        /// <summary>
        /// init
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click_1(object sender, EventArgs e)
        {

            Common.HaredWareCommonM haredWareCommonM = new Common.HaredWareCommonM();
            haredWareCommonM.PortName = comboBox1.SelectedItem.ToString();
            haredWareCommonM.BaudRate = Convert.ToInt32(comboBox2.SelectedItem.ToString());
            haredWareCommonM.DataBits = Convert.ToInt32(comboBox3.SelectedItem.ToString());
            haredWareCommonM.StopBits = (StopBits)(Convert.ToInt32(comboBox4.SelectedIndex.ToString()));
            haredWareCommonM.Parity = (Parity)(Convert.ToInt32(comboBox5.SelectedIndex.ToString()));
            haredWareCommonM.Port = textBoxPort.Text;
            NBKLD39WagonBalance = new HardWareBLL.A9WagonBalanceBLL(haredWareCommonM);
            if (socketServerBLL == null)
            {
                socketServerBLL = new SocketServerBLL(haredWareCommonM);
            }

            Common.DelegateHelper.A9Event += WeightCallback;
            Common.DelegateHelper.InfoMessageEvent += aa;
            Common.ShowBlackBox.WriteLine("1 init success");


             Myconfig.myconfigValue.DuankouhaoIndex = comboBox1.SelectedIndex;
            Myconfig.myconfigValue.BotelvIndex = comboBox2.SelectedIndex;
           Myconfig.myconfigValue.ShujuweiIndex = comboBox3.SelectedIndex ;
            Myconfig.myconfigValue.TingzhiweiIndex = comboBox4.SelectedIndex ;
           Myconfig.myconfigValue.XiaoyanweiIndex = comboBox5.SelectedIndex ;
            Myconfig.myconfigValue.Port = textBoxPort.Text ;
            Myconfig.WhriteMyconfigM();
        }

        private void aa(string str)
        {
            this.Invoke(new Action(() =>
            {
                int leg = textBox2.TextLength;
                string infoTest = textBox2.Text;
                if (leg > 5000)
                {
                    infoTest = infoTest.Substring(leg / 5);
                    textBox2.Text = infoTest;
                }
                textBox2.AppendText(DateTime.Now.ToString("HH:mm:ss")+" "+str + "\r\n");
            }));
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Common.DelegateHelper.A9Event -= WeightCallback;
            Common.DelegateHelper.InfoMessageEvent -= aa;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Common.DelegateHelper.A9Event += WeightCallback;
            Common.DelegateHelper.InfoMessageEvent += aa;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            NBKLD39WagonBalance.close();
            Common.DelegateHelper.A9Event -= WeightCallback;
            Common.DelegateHelper.InfoMessageEvent -= aa;
        }

        private void 地磅串口转tcp_Load(object sender, EventArgs e)
        {
            if (AppConfigHelper.GetAppSettings("DBCONZTCP")== "1")
            {
                button1_Click_1(null, null);
            }
           
        }
    }
}
